package com.zlzxm.shoesmarket.viewmodel;

import androidx.lifecycle.MutableLiveData;

import com.zlzxm.shoesmarket.globle.UserManager;
import com.zlzxm.shoesmarket.net.RequestCode;
import com.zlzxm.shoesmarket.net.RequestTip;
import com.zlzxm.shoesmarket.net.repository.UserRepository;
import com.zlzxm.shoesmarket.net.request.RqLogin;
import com.zlzxm.shoesmarket.net.respone.BaseResponse;
import com.zlzxm.shoesmarket.net.respone.RpLogin;
import com.zlzxm.shoesmarket.util.RxAndroidHelp;
import com.zlzxm.shoesmarket.util.ZStringHelp;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.rxjava3.functions.Consumer;

/**
 * Created by zlz
 * on  5/11/21
 */
public class VMLogin extends BaseViewModel{

    UserRepository userRepository = new UserRepository();

    public MutableLiveData<Boolean> liveIsCodeLogin = new MutableLiveData<>(true);

    public MutableLiveData<Boolean> liveIsBtnEnable = new MutableLiveData<>(false);

    public MutableLiveData<String> livePhone = new MutableLiveData<>();

    public MutableLiveData<String> livePwd = new MutableLiveData<>();

    public MutableLiveData<String> liveCode = new MutableLiveData<>();



    public  void updateInput(String phone,String code,String pwd){

        livePhone.setValue(phone);
        livePwd.setValue(code);
        liveCode.setValue(pwd);

        liveIsBtnEnable.setValue(liveIsCodeLogin.getValue() ? !ZStringHelp.isEmpty(phone, code) : !ZStringHelp.isEmpty(phone, pwd));


    }


    public void requestLogin(String phone,String pwd){

        RqLogin rqLogin = new RqLogin(pwd,phone);

        userRepository.login(rqLogin)
                .compose(RxAndroidHelp.turn())
                .subscribe(new Consumer<BaseResponse<RpLogin>>() {
                    @Override
                    public void accept(BaseResponse<RpLogin> response) throws Throwable {

                        if (response.getCode() == RequestCode.REQUEST_SUCCEED){

                            UserManager.getInstance().login(response.getData().getUserLoginInfo().getId()
                                    ,response.getData().getUserLoginInfo().getToken());

                            List<UserManager.Repository> list = new ArrayList<>();

                            for (RpLogin.WareHouse warehouse : response.getData().getWarehouses()) {

                                UserManager.Repository repository = new UserManager.Repository();
                                repository.setId(warehouse.getId());
                                repository.setName(warehouse.getCkname());

                                list.add(repository);
                            }

                            UserManager.getInstance().getUser().setRepositories(list);

                            liveSucceedToast.setValue("登录成功");

                        }else {

                            liveErrorToast.setValue(response.getMsg());
                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Throwable {
                        throwable.printStackTrace();
                        liveErrorToast.setValue(RequestTip.REQUEST_ERROR);
                    }
                });


    }


}
